package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.domain.AdminSmAuthReco;
import cn.com.yusys.yusp.admin.domain.AdminSmAuthRecoPojo;
import cn.com.yusys.yusp.admin.service.AdminSmAuthRecoService;
import cn.com.yusys.yusp.admin.service.AdminSmOrgService;
import cn.com.yusys.yusp.admin.service.AdminSmRoleService;
import cn.com.yusys.yusp.admin.service.AdminSmUserService;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.IoResourceFactory;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @class AdminSmAuthRecoResource.java
 * @author 作者 :hujun
 * @version 创建时间：Dec 15, 2017 5:47:27 PM
 * @Drc 资源对象关系配置
 */

/**
 * @version 1.0.0
 * @项目名称: yusp-admin
 * @类名称: AdminSmAuthRecoResource
 * @类描述:
 * @功能描述: 资源对象关系配置
 * @创建人: hujun@yusys.com.cn
 * @创建时间: 2017-12-20 17:18
 * @修改备注:
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @Copyright (c) 2018宇信科技-版权所有
 */
@RestController
@RequestMapping("/api/adminsmauthteco")
public class AdminSmAuthRecoResource extends CommonResource<AdminSmAuthReco, String> {
    @Autowired
    private AdminSmAuthRecoService service;
    @Autowired
    private AdminSmRoleService roleService;
    @Autowired
    private AdminSmUserService userService;
    @Autowired
    private AdminSmOrgService orgService;

    @Autowired
    private IoResourceFactory factory;

    private Logger logger = LoggerFactory.getLogger(AdminSmAuthRecoService.class);

    @Override
    protected CommonService getCommonService() {
        return this.service;
    }

    /**
     * @方法名称: getRecoInfo
     * @方法描述: 查询对象资源关系数据
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/queryinfo")
    @ResponseBody
    public ResultDto<List<Map<String, Object>>> getRecoInfo(@RequestParam(required = false) String objectType,
                                                            @RequestParam(required = false) String resType,
                                                            @RequestParam(required = false) String objectId, @RequestParam(required = false) String sysId) {
        logger.info("Query object resource relationship data");
        List<Map<String, Object>> list = service.getList(objectType, resType, objectId, sysId);
        return new ResultDto<List<Map<String, Object>>>(list);
    }

    /**
     * @方法名称:selectMenuTree
     * @方法描述:菜单树初始化查询
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/menutreequery")
    public ResultDto<List<Map<String, Object>>> selectMenuTree(String sysId,boolean lazy,String id) {
        List<Map<String, Object>> list = service.selectMenuTree(sysId,lazy,id);
        return new ResultDto<List<Map<String, Object>>>(list.size(), list);
    }

    /**
     * @方法名称:selectMenuTree
     * @方法描述:数据权限树初始化查询
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/datapowertreequery")
    public ResultDto<List<Map<String, Object>>> selectDataPowerTree(String sysId) {
        List<Map<String, Object>> list = service.selectDataPowerTree(sysId);
        return new ResultDto<List<Map<String, Object>>>(list.size(), list);
    }

    /**
     * @函数名称:批量保存数据
     * @参数与返回说明:
     * @算法描述:
     */
    @SuppressWarnings("unchecked")
    @PostMapping("/saveinfo")
    public ResultDto<Object> saveInfo(@RequestBody Map<?, ?> t)
            throws Exception {
        int result = 0;
        if (t != null) {
            List<Map<String, Object>> menus = (List<Map<String, Object>>) t.get("menuData");
            List<Map<String, Object>> ctrs = (List<Map<String, Object>>) t.get("ctrData");
            result = service.insertBatch(menus, ctrs);
            logger.info("Modify object resource relationship data");
        }

        return new ResultDto<Object>(result);
    }

    /**
     * @函数名称:批量保存数据
     * @参数与返回说明:
     * @算法描述:
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/savedatapowerinfo")
    public ResultDto<Object> saveDataPowerInfo(@RequestBody ArrayList<AdminSmAuthReco> t)
            throws Exception {
        logger.debug("Modify object resource relationship data");
        ResultDto resultDto = new ResultDto();
        int result = 0;
        List<AdminSmAuthReco> list = t;
        result = service.saveRoleDataPower(list);
        resultDto.setData(result);
        resultDto.setMessage("Modifying object resource relation data succeeded!");
        return resultDto;
    }

    /**
     * @函数名称:复制保存
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/savecopyinfo")
    public ResultDto<Object> saveCopyInfo(@RequestBody Map<?, ?> t)
            throws Exception {
        Map<?, ?> list = t;
        int result = service.saveCopyRelate(list);
        logger.info("Copy object resource relationship data");
        return new ResultDto<Object>(result);
    }

    /**
     * @函数名称:复制保存数据权限
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/savecopydatapowerinfo")
    public ResultDto<Object> saveCopyDataPowerInfo(@RequestBody Map<?, ?> t) {
        logger.debug("Copy object resource relationship data");
        Map<?, ?> list = t;
        ResultDto<Object> resultDto = new ResultDto<>(service.saveCopyDataPower(list));
        resultDto.setMessage("Copying object resource relation data succeeded!");
        return resultDto;
    }

    @GetMapping("/export")
    public void export(QueryModel queryModel, HttpServletRequest request, HttpServletResponse response) throws IOException {
        factory.export(queryModel, request, response, "exportAuthReco", AdminSmAuthRecoPojo.class.getName());
    }


    /**
     * 异步导出
     *
     * @param queryModel
     * @param fetchSize
     * @return
     */
    @GetMapping("/exportasync")
    public String asyncExport(QueryModel queryModel, Integer fetchSize) {
        return factory.asyncExport(queryModel, fetchSize, "exportAuthReco", AdminSmAuthRecoPojo.class.getName());
    }

    /**
     * @param map
     * @return 授权数据删除count
     */
    @PostMapping("/deletedatainfo")
    public ResultDto<Integer> deleteDatainfo(@RequestBody Map<String, Object> map) {
        ResultDto<Integer> resultDto = new ResultDto<Integer>(service.deleteDatainfo(map));
        resultDto.setMessage("Data deleted successfully!");
        return resultDto;
    }

    /**
     * 根据状态只查询生效角色
     */
    @GetMapping("/querybyrolests")
    public ResultDto<List<Map<String, Object>>> getListByRolests(QueryModel queryModel) {
        List<Map<String, Object>> list = roleService.queryPage(queryModel);
        return new ResultDto<List<Map<String, Object>>>(list);
    }

    /**
     * @方法名称: getList
     * @方法描述: 分页查询
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/querypage")
    public ResultDto<List<Map<String, Object>>> getList(QueryModel queryModel) {
        List<Map<String, Object>> list = userService.findAllByParam(queryModel);
        return new ResultDto<List<Map<String, Object>>>(list);
    }

    /**
     *
     * @方法名称: orgTreeQuery
     * @方法描述:  机构树查询
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/orgtreequery")
    public ResultDto<Object> orgTreeQuery(@RequestParam(required = false) String orgId,
                                          @RequestParam(required = false) String orgSts,@RequestParam(required = false) Boolean lazy) {
        logger.info("Query organization tree");
        List<Map<String, Object>> list=orgService.getOrgTree(orgId,orgSts,lazy);

        return new ResultDto<Object>(list.size(),list);
    }

}
